#ifndef ANALYSISWIDGET_H
#define ANALYSISWIDGET_H

#include <QWidget>
#include <QProcess>
#include <QMap>
#include <QStringList>
#include <QPainter>
#include <QLineEdit>
#include <QComboBox>
#include <QCheckBox>
#include <QTextEdit>
#include <QPushButton>
#include <QStackedLayout>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QtMath>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QGroupBox>
#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QPixmap>
#include <QFont>
#include <QPen>
#include <QBrush>
#include <QRect>
#include <QRectF>
#include <QPointF>
#include <QVector>
#include <QPair>
#include <QColor>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>
#include <QPdfWriter>
#include <QPageSize>
#include <QDesktopServices>
#include <QUrl>
#include <algorithm>

class AnalysisWidget : public QWidget
{
    Q_OBJECT

public:
    explicit AnalysisWidget(QWidget *parent = nullptr);

    // 设置分析参数的公共接口
    void setAnalysisMethod(const QString& method);
    void setFFTParameters(int windowSize, const QString& windowType, int overlap);
    void setWaveletParameters(const QString& waveletType, int level);
    void setMLParameters(const QString& algorithm, const QString& featureSelection, int crossValidation);
    void setDLParameters(const QString& architecture, int epochs, int batchSize, double learningRate);
    void setSaveResults(bool save);
    void setOutputFormat(const QString& format);
    void setGenerateReport(bool generate);

    // 报告生成的公共接口
    void generateReport();

signals:
    // 信号定义
    void trainingStarted(const QString& details);
    void inferenceStarted(const QString& details);

public slots:
    // 用户交互的槽函数
    void onStartTraining();
    void onStartInference();
    void onProcessOutput();
    void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void onSelectModel();
    void onSelectEEGDataset(int index);
    void onSelectECGDataset(int index);
    void onSelectGSRDataset(int index);
    void validateModelDatasetMatch(int datasetIndex);

private:
    // UI初始化和辅助函数
    void initUI();
    void initConnections();
    void loadAvailableDatasets();
    void createFFTParamUI();
    void createWaveletParamUI();
    void createMLParamUI();
    void createDLParamUI();
    void updateUIForAnalysisMethod();
    void setControlsEnabled(bool enabled);

    // 报告生成相关函数 - 原有函数
    void drawReportHeader(QPainter &painter, int &yPos, const QMap<QString, QString> &subjectInfo);
    void drawWaveforms(QPainter &painter, int &yPos);
    void drawAnalysisResults(QPainter &painter, int &yPos);
    void drawTreatmentSuggestions(QPainter &painter, int &yPos);
    void drawDeviceInfo(QPainter &painter, int &yPos);
    void drawSectionHeader(QPainter &painter, int &yPos, const QString &title);
    void drawLogo(QPainter &painter, int &yPos);

    // 报告生成相关函数 - 新增函数
    void drawScoreChart(QPainter& painter, const QVector<double>& scores, int recordNum);
    void drawWaveformsFromDB(QPainter& painter, int& yPos);
    void drawSingleWaveform(QPainter& painter, int& yPos, const QVector<double>& data,
                            const QColor& color, const QString& title);
    void drawResultBackground(QPainter& painter, int resultType);
    void drawBackgroundTemplate(QPainter& painter, const QString& gender);
    void drawBasicInfo(QPainter& painter, int& yPos, const QMap<QString, QString>& subjectInfo);
    void drawCurrentTestResult(QPainter& painter, int& yPos, double score, int resultType);
    void drawSectorChart(QPainter& painter, int& yPos, const QMap<QString, double>& testScores, int recordNum);

    // 数据处理函数
    QMap<QString, QVector<double>> applyBandpassFilter(const QMap<QString, QVector<QPair<qint64, double>>>& rawData);
    QVector<double> simpleBandpassFilter(const QVector<double>& input, double fs, double lowFreq, double highFreq);

    // 辅助函数
    QString getScoreColor(double score);
    QPointF calculateLabelPosition(int centerX, int centerY, int index, int offset);
    int getCurrentSubjectId();
    int getCurrentRecordNum();
    double getCurrentTestScore(const QMap<QString, double>& testScores, int recordNum);
    int calculateResultType(double score);
    bool validateReportData(const QMap<QString, QString>& subjectInfo, const QMap<QString, double>& testScores);

    // 数据获取函数
    QMap<QString, QString> getSubjectInfo();
    QMap<QString, QString> getSubjectInfoFromDB();
    QMap<QString, double> getAnalysisResults();
    QMap<QString, double> getTestScoresFromDB();
    QStringList getTreatmentSuggestions();
    QMap<QString, QVector<QPair<qint64, double>>> getWaveformDataFromDB();

private:
    // 界面控件
    QLineEdit* m_modelEdit = nullptr;
    QComboBox* m_eegDatasetCombo = nullptr;
    QComboBox* m_ecgDatasetCombo = nullptr;
    QComboBox* m_gsrDatasetCombo = nullptr;
    QStackedLayout* m_paramLayout = nullptr;
    QCheckBox* m_saveResultsCheck = nullptr;
    QComboBox* m_outputFormatCombo = nullptr;
    QCheckBox* m_generateReportCheck = nullptr;
    QPushButton* m_trainButton = nullptr;
    QPushButton* m_inferenceButton = nullptr;
    QTextEdit* m_outputEdit = nullptr;

    // 进程控制
    QProcess* m_process = nullptr;

    // 参数设置
    QString m_selectedModel;
    QString m_eegDataset;
    QString m_ecgDataset;
    QString m_gsrDataset;
    QString m_analysisMethod = "FFT";  // 默认使用FFT分析

    // FFT参数
    int m_fftWindowSize = 1024;
    QString m_fftWindowType = "Hamming";
    int m_fftOverlap = 50;

    // 小波参数
    QString m_waveletType = "Daubechies";
    int m_waveletLevel = 5;

    // 机器学习参数
    QString m_mlAlgorithm = "SVM";
    QString m_mlFeatureSelection = "PCA";
    int m_mlCrossValidation = 5;

    // 深度学习参数
    QString m_dlArchitecture = "CNN";
    int m_dlEpochs = 100;
    int m_dlBatchSize = 32;
    double m_dlLearningRate = 0.001;

    // 输出参数
    bool m_saveResults = true;
    QString m_outputFormat = "CSV";
    bool m_generateReportBool = true;  // 重命名以避免与函数名冲突

    QSqlDatabase getDatabase();
};

#endif // ANALYSISWIDGET_H
